package dat255.calendarclock.view;

import java.util.Random;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.widget.CompoundButton;
import android.widget.ToggleButton;

/**
 * A button that filters categories
 * @author Marcus Hultman, Gabriel Andersson
 *
 */
public class FilterButton extends ToggleButton {
	
	//Fields
	
	private static Paint backgroundPaint = new Paint();
	private static Paint textPaint = new Paint();
	
	private static int[] colorChoices = new int[]{
			Color.RED,
			Color.BLUE,
			Color.YELLOW,
			Color.GREEN,
	};
	private static Random random = new Random();
	
	private Paint colorPaint;
	private String label;
	
	public FilterButton(Context context) {
		super(context);
		setWillNotDraw(false);
		setVisible(false);

		backgroundPaint.setColor(Color.LTGRAY);
		textPaint.setColor(Color.BLACK);
		
		colorPaint = new Paint();
		colorPaint.setColor(colorChoices[random.nextInt(colorChoices.length)]);
		
		label = "Untiteled";
		
		setOnCheckedChangeListener(new OnCheckedChangeListener() {
			
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				//invalidate();	
			}
		});
	}
	
	public void setVisible(boolean visible){
		setVisibility(visible ? VISIBLE : INVISIBLE);
	}

	
	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
		int sideLength = getSuggestedMinimumWidth() - FilterBar.WIDTH_HIDDEN - 1;
		setMeasuredDimension(sideLength, sideLength);
	}
	
	@Override
	protected void onDraw(Canvas canvas) {
		Rect boundingRect = new Rect(0, 0, 
				getWidth(), getHeight());
		if (isChecked()){
			canvas.drawRect(boundingRect, colorPaint);
		}	
		else {
			canvas.drawRect(boundingRect, backgroundPaint);
			boundingRect.right /= 10;
			canvas.drawRect(boundingRect, colorPaint);
		}
		canvas.drawText(label, textPaint.getFontSpacing(), 
				getHeight() - 2, textPaint);
	}
}
